//
//  Credits.m
//  GameProject
//
//  Created by Blair Trusler on 20/02/2012.
//  Copyright (c) 2012 Blair Trusler. All rights reserved.
//

#import "GameHeader.h"

@implementation Credits

#pragma mark - scene
+(id) scene
{
    CCScene *scene = [CCScene node];
    
    Credits *layer = [Credits node];
    
    [scene addChild: layer];
    
    return scene;
}


#pragma mark - initialize
-(id) init
{
    if( (self=[super init] )) {
        CCLOG(@"Loading Credits...");
        
        [self loadScreenContents];
        [self rewardAchievements];

        CCLOG(@"Credits loaded.");
    }
    return self;
}


-(void)loadScreenContents{
    CGSize winSize = [CCDirector sharedDirector].winSize;
    
//    CCSprite *backgroundImage = [CCSprite spriteWithFile:@"GameBackground.png"];
    CCSprite *creditsTitle = [CCSprite spriteWithFile:@"Credits.png"];
    CCSprite *creditsFull = [CCSprite spriteWithFile:@"CreditsFull.png"];
        
    CCSprite *poweredBy = [CCSprite spriteWithFile:@"PoweredBy.png"];
    CCSprite *cocos2DLogo = [CCSprite spriteWithFile:@"Cocos2DLogo.png"];
    
    
    CCMenuItemImage *returnToTitleScreen = [CCMenuItemImage itemFromNormalImage:@"backbutton.png" selectedImage:@"backbutton.png" target:self selector:@selector(returnToTitleScreen:)];
    CCMenu *returnToTitleScreenMenu = [CCMenu menuWithItems:returnToTitleScreen, nil];

    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        returnToTitleScreen.scale = 0.6f;
        returnToTitleScreenMenu.position = ccp(50 , 720);
        creditsTitle.position = ccp(winSize.width/2 ,700);
        creditsFull.position = ccp(winSize.width/2, 400);
        poweredBy.position = ccp(375,100);
        cocos2DLogo.position = ccp(675,100);
    }
    else{
        returnToTitleScreen.scale = 0.4f;
        returnToTitleScreenMenu.position = ccp(26,winSize.height*0.9);

//        returnToTitleScreenMenu.scale = 0.75f;
//        returnToTitleScreenMenu.position = ccp(-20,winSize.height*0.78);
        creditsTitle.scale = 0.75f;
        creditsTitle.position = ccp(winSize.width/2 ,winSize.height*0.85);
        creditsFull.scale = 0.5f;
        creditsFull.position = ccp(winSize.width/2, winSize.height*0.45);
        poweredBy.scale = 0.5f;
        cocos2DLogo.scale = 0.5f;
        poweredBy.position = ccp(175,20);
        cocos2DLogo.position = ccp(325,20);


    }
    [self addChild:poweredBy];
    [self addChild:cocos2DLogo];
    [self addChild:creditsTitle];
    [self addChild:creditsFull];
    [self addChild:returnToTitleScreenMenu];
}

-(void)returnToTitleScreen:(id)sender{
    [AudioManager playMenuButtonClick];
    [[CCDirector sharedDirector] popScene]; //'pops' back to previous scene    

//    [AudioManager playMenuButtonClick];
//    [[SceneManager sharedSceneManager] runSceneWithID:kMainMenuScene];    
}

-(void)rewardAchievements{
    //Award achievement 
    if (![GameState sharedInstance].achievement8){
        [GameState sharedInstance].achievement8 = TRUE;
        [[GameState sharedInstance]save];
        [[GameCenterLogin sharedInstance] reportAchievement:kAchievement8 percentComplete:100.0];
//        NSString *achievementName = [NSString stringWithFormat:kAchievement8];
//        CCLOG(@"Awarded Achievement - %@",achievementName);
    }
}

@end
